// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "./gameData";
import mainManage from "../framework/mainManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class openBox extends cc.Component {

    @property(cc.Node)
    boxNode : cc.Node = null;

    @property(cc.Node)
    appleNode : cc.Node = null;

    @property(cc.Node)
    lightNode : cc.Node = null;

    @property(cc.ProgressBar)
    progressBar : cc.ProgressBar = null;

    up = 10;

    down = 0.5;

    curProgress = 5;

    bannerInterval = [40,70];

    randomInterval = -1;

    showBanner = false;

    stopReduce = false;

    showBox = false;

    index = 0;

    onLoad () {
        this.progressBar.progress = this.curProgress / 100;
        this.randomBannerInterval();
        this.node.getChildByName("btnClick").on("touchstart",this.TouchStart,this);
        this.schedule(()=>{
            if(this.add) return;
            this.btnShake();
        },2)
    }

    randomBannerInterval(){
        this.randomInterval =  this.bannerInterval[0] + Math.random() * (this.bannerInterval[1] - this.bannerInterval[0] + 1) | 0;
    }

    tweenAction = null;
    btnShake(){
       this.tweenAction = cc.tween(this.node.getChildByName("btnClick"))
           .to(0.3,{scale : 1.1})
           .to(0.3,{scale : 1})
           .start();
    }

    hide = null;
    add = false;


    TouchStart(event){
        this.add = true;
        if(this.tweenAction != null){
            this.tweenAction.stop();
        }
    }

    addProgress(event){
        if(this.stopReduce) return;
        this.add = false;
        this.curProgress += this.up;
        let spk = this.boxNode.getComponent(sp.Skeleton);
        spk.setAnimation(0,"box_ani2",false);
         if(this.curProgress >= this.randomInterval){

             if(this.showBanner || this.showBox) return;
             this.showBanner = true;
             if(this.showBox) return;
             this.showBox = true;
             let spk = this.boxNode.getComponent(sp.Skeleton);
             spk.setAnimation(0,"box_ani3",false);
             this.appleNode.active = true;
             gameData.addSinglePumpkin();
             mainManage.createToast("恭喜获得一个苹果");
             this.scheduleOnce(()=>{
                 this.node.destroy();
                 if(this.callback != null){
                     this.callback();
                 }
             },1);
        }
    }

    protected onDestroy(): void {

    }

    callback = null;

    setCallBack(callback){
        this.callback = callback;
    }

    update (dt) {
        if(this.stopReduce) return;
        if(this.curProgress >= 100){
            this.stopReduce = true;
            this.curProgress = 100;
            this.progressBar.progress = this.curProgress / 100;

            if(this.showBox) return;
            this.showBox = true;
            let spk = this.boxNode.getComponent(sp.Skeleton);
            spk.setAnimation(0,"box_ani3",false);
            this.appleNode.active = true;
            gameData.addSinglePumpkin();
            mainManage.createToast("恭喜获得一个苹果");
            this.scheduleOnce(()=>{
                this.node.destroy();
                if(this.callback != null){
                    this.callback();
                }
            },1);
            return;
        }
        if(this.curProgress <= 0){
            this.curProgress = 0;
        }else{
            this.curProgress -= this.down;
            if(this.curProgress <= 0){
                this.curProgress = 0;
            }
        }
        this.progressBar.progress = this.curProgress / 100;
    }
}
